1 /*
2  * The MIT License (MIT)
3  *
4  * Copyright (c) 2014 Devisualization (Richard Andrew Cattermole)
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 module devisualization.util.opengl.renderbuffer;
25 import devisualization.util.opengl.function_wrappers;
26 import devisualization.util.opengl.texture;
27 import glINCOMPLETE = derelict.opengl3.gl3;
28 deprecated("de_util:opengl is going to die"):
29 
30 struct RenderBuffer {
31 	private {
32 		uint id_;
33 		uint width_, height_;
34 	}
35 
36 	this(uint width, uint height, InternalFormat format) {
37 		width_ = width;
38 		height_ = height;
39 
40 		glINCOMPLETE.glGenRenderbuffers(1, &id_);
41 		bind();
42 		glINCOMPLETE.glRenderbufferStorage(glINCOMPLETE.GL_RENDERBUFFER, format, width, height);
43 	}
44 
45 	~this() {
46 		glINCOMPLETE.glDeleteRenderbuffers(1, &id_);
47 	}
48 
49 	void bind() {
50 		glINCOMPLETE.glBindRenderbuffer(glINCOMPLETE.GL_RENDERBUFFER, id_);
51 	}
52 
53 	@property {
54 		uint id() {
55 			return id_;
56 		}
57 
58 		uint width() {
59 			return width_;
60 		}
61 
62 		uint height() {
63 			return height_;
64 		}
65 	}
66 
67 	/**
68 	 * 
69 	 * Params:
70 	 * 		x	=	Default: 0 (left)
71 	 * 		y	=	Default: height (top), use 0 for bottom
72 	 * 
73 	 * Returns:
74 	 * 		TODO: fix data type, should really be an Image!
75 	 */
76 	ubyte[] readData(uint x = 0, uint y = height_) {
77 		return cast(ubyte[])glReadPixels(x, y, width_, height_, ReadBlockPixelsFormat.RGBA, ReadBlockPixelsType.UnsignedByte);
78 	}
79 
80 	/**
81 	 * 
82 	 * Params:
83 	 * 		x	=	Default: 0 (left)
84 	 * 		y	=	Default: height (top), use 0 for bottom
85 	 */
86 	float[] readDepthComponent(uint x = 0, uint y = height_) {
87 		return cast(float[])glReadPixels(x, y, width_, height_, ReadBlockPixelsFormat.DepthComponent, ReadBlockPixelsType.Float);
88 	}
89 
90 	static {
91 		void unbind() {
92 			glINCOMPLETE.glBindRenderbuffer(glINCOMPLETE.GL_RENDERBUFFER, 0);
93 		}
94 	}
95 }